package dhome.tasks;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.HashMap;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class QuartzUA {
	private static Logger log = LoggerFactory.getLogger(QuartzUA.class);
	private final SchedulerFactory sf=new StdSchedulerFactory();
	private Scheduler sched;
	private final HashMap<String,JobDetail> jobs=new HashMap<String,JobDetail>();

	public QuartzUA(){}
	
	public void startup() throws Exception{
		if(sched==null||sched.isShutdown())
			sched=sf.getScheduler();
		
	}
	
	public void shutdown() throws Exception{
		sched.shutdown();
	}
	
	public void start_job(QuartzJobDetail jobd) throws Exception{		
		CronTrigger trigger=newTrigger()
	        .withIdentity(jobd.getName(), jobd.getGroup())
	        .withSchedule(cronSchedule(jobd.get_schedule()))
	        .build();			
		
		sched.scheduleJob(jobd,trigger);		
		if(!sched.isStarted())
		   sched.start();
	}
	
	public void stop_job(QuartzJobDetail job) throws Exception{
		//sched.deleteJob(job.getKey());
	}
	
}
